<?php

/**
 * crpLicense
 *
 * @copyright (c) 2009, Daniele Conca
 * @link http://code.zikula.org/crplicense Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpLicense
 */

/**
 * crpLicenseUI Object
 */
class crpLicenseUI
{

	function crpLicenseUI()
	{
		//
	}

	function newItemLicense($licenseChoosed= null, $modvars= array (), $licenseArray= array ())
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		$pnRender->assign($modvars);
		$pnRender->assign('licenseArray', $licenseArray);

		// Return the output that has been generated by this function
		return $pnRender->fetch('hooks/crplicense_user_newlicense.htm');
	}

	function modifyItemLicense($licenseChoosed= null, $modvars= array (), $licenseArray= array ())
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		$pnRender->assign('licenseChoosed', $licenseChoosed);
		$pnRender->assign($modvars);
		$pnRender->assign('licenseArray', $licenseArray);

		// Return the output that has been generated by this function
		return $pnRender->fetch('hooks/crplicense_user_modifylicense.htm');
	}

	function displayItemLicense($licenseChoosed= null, $modvars= array (), $returnurl= null)
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		$pnRender->assign($modvars);
		$pnRender->assign('licenseChoosed', $licenseChoosed);
		$pnRender->assign('returnurl', $returnurl);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crplicense_user_embedlicense.htm');
	}

	function drawAdminView($rows= array (), $active= null, $modvars= array (), $pager= array ())
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		// Assign the items to the template
		$pnRender->assign('licenses', $rows);
		$pnRender->assign('licenses_status', $active);
		$pnRender->assign($modvars);

		// Assign the information required to create the pager
		$pnRender->assign('pager', $pager);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crplicense_admin_view.htm');
	}

	/**
	 * draw creation form
	 *
	 * @param array $item of event's value
	 * @param array $modvars module's variables
	 *
	 * @return string html
	 */
	function newLicense($item= array (), $modvars)
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		$pnRender->assign($modvars);
		$pnRender->assign($item);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crplicense_admin_new.htm');
	}

	/**
	 * draw modify form
	 *
	 * @param array $item of event's value
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param array $avail available locations
	 *
	 * @return string html
	 */
	function modifyLicense($item= array (), $modvars= array ())
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		$pnRender->assign($modvars);

		// assign the item to the template
		$pnRender->assign('id', $item['id']);
		$pnRender->assign($item);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crplicense_admin_modify.htm');
	}

	/**
	 * draw modify configuration form
	 *
	 * @param array $gdArray gd library info
	 * @param array $modvars module's variables
	 *
	 * @return string html
	 */
	function modifyConfig($modvars= array (), $gdArray= array ())
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		$pnRender->assign($modvars);
		$pnRender->assign('gd_version', $this->gd_version($gdArray['GD Version']));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crplicense_admin_modifyconfig.htm');
	}

	/**
	 * calculate gd library information
	 *
	 * @return array gd library info
	 */
	function gd_version($fullstring= null)
	{
		$cache_gd_version= array ();

		if (eregi('bundled \((.+)\)$', $fullstring, $matches))
		{
			$cache_gd_version['string']= $fullstring; // e.g. "bundled (2.0.15 compatible)"
			$cache_gd_version['value']= (float) $matches[1]; // e.g. "2.0" (not "bundled (2.0.15 compatible)")
		}
		else
		{
			$cache_gd_version['string']= $fullstring; // e.g. "1.6.2 or higher"
			$cache_gd_version['value']= (float) substr($fullstring, 0, 3); // e.g. "1.6" (not "1.6.2 or higher")
		}

		return $cache_gd_version;
	}
}